package com.crm.controller.permission;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.exception.ServiceException;
import com.crm.pojo.CrmDept;
import com.crm.pojo.Userinfo;
import com.crm.service.permission.PowerService;
import com.crm.vo.Result;

@Controller
public class PowerController {

	@Autowired
	private PowerService powerService;

	/**
	 * 查询所有部门
	 * 
	 * @return
	 */
	@GetMapping("to-findAllCrmDept")
	@ResponseBody
	public List<CrmDept> tofindAllCrmDept() {
		return powerService.findAllCrmDept();
	}

	/**
	 * 分页查询所有部门
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("queryAllPage")
	@ResponseBody
	public Map queryAllPage(int page, int rows) {
		Long total = powerService.queryPageCount();
		List<CrmDept> list = powerService.queryPage(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	/**
	 * 添加部门
	 * 
	 * @param crmDname
	 */
	@PostMapping("add-OneCrmDept")
	@ResponseBody
	public Result addOneCrmDept(String crmDname) {

		try {
			CrmDept crmDept = powerService.findOneCrmDept(crmDname);
			if (null == crmDept) {
				powerService.AddCrmDept(crmDname);
				return Result.SUCCESS;
			} else {
				return new Result("添加失败，部门名称重复！");
			}

		} catch (ServiceException e) {
			e.printStackTrace();
			return new Result("添加失败，部门名称重复！");
		}
	}

	/**
	 * 根据部门名称模糊查询
	 * 
	 * @param crmDname
	 * @return
	 */
	@PostMapping("query-Some-CrmDepts")
	@ResponseBody
	public Map querySomeCrmDepts(String crmDname, int page, int rows) {
		Long total = powerService.queryPageNmaeCount(crmDname);
		List<CrmDept> list = powerService.querySomeCrmDept(crmDname, page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	/**
	 * 逻辑删除一个部门
	 * 
	 * @param crmDeptno
	 */
	@PostMapping("deleting-dept")
	@ResponseBody
	public Result deleteOneCrmDept(Long crmDeptno) {
//		 System.out.println("请输出"+crmDeptno);
		try {
			List<Userinfo> list = powerService.findDeptUsers(crmDeptno);
			int user = 0;
			for (int i = 0; i < list.size(); i++) {
				user++;
			}
			if(user == 0){
				powerService.deleteOneCrmDept(crmDeptno);
				return Result.SUCCESS;
			}else{
				return new Result("删除失败，部门中存在员工");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(e);
		}
	}

	@GetMapping("init")
	@ResponseBody
	public CrmDept init(Long crmDeptno) {
		CrmDept cd = powerService.queryOne(crmDeptno);
		return cd;
	}

	/**
	 * 修改部门信息
	 * 
	 * @param crmDeptno
	 * @param crmDname
	 */
	@PostMapping("update-One-CrmDepts")
	@ResponseBody
	public Result updateOneCrmDepts(Long crmDeptno, String crmDname) {
		try {
			CrmDept Dept = powerService.queryOne(crmDeptno);
			CrmDept crmDept = powerService.findOneCrmDept(crmDname);
			if(Dept.getCrmDname().equals(crmDname)){
				powerService.updateOneCrmDept(crmDeptno, crmDname);
				return Result.SUCCESS;
			}else if (null == crmDept) {
				powerService.updateOneCrmDept(crmDeptno, crmDname);
				return Result.SUCCESS;
			} else {
				return new Result("修改失败，部门名称重复！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result("修改失败，部门名称重复！");
		}
	}

	@PostMapping("submit")
	@ResponseBody
	public Map submit(String crmDname) {
		Map map = new HashMap<>();
		map.put("success", true);
		return map;
	}

	@GetMapping("crmdept")
	public String toCrmDept() {
		return "permission/crm-dept";
	}

	@GetMapping("to-add-dept")
	public String toAddDept() {
		return "permission/add-dept";
	}

	@GetMapping("update-dept")
	public String toUpdateDept() {
		return "permission/update-dept";
	}
	
	
	
}
